package com.jiugang.cardump.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiugang.cardump.bean.InformationBean;
import com.jiugang.cardump.constant.StatusCodeConstant;
import com.jiugang.cardump.entity.Alert;
import com.jiugang.cardump.entity.Monitor;
import com.jiugang.cardump.exception.SystemExceptionV110;
import com.jiugang.cardump.mapper.AlertMapper;
import com.jiugang.cardump.mapper.MonitorMapper;
import com.jiugang.cardump.pojo.CarDumperPOJO;
import com.jiugang.cardump.pojo.MonitorPagePOJO;
import com.jiugang.cardump.pojo.MonitorParamPOJO;
import com.jiugang.cardump.result.ResultInfoV110;
import com.jiugang.cardump.service.MonitorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiugang.cardump.util.PageUtil;
import com.jiugang.cardump.util.TypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2021-08-19
 */
@Service
@Slf4j
public class MonitorServiceImpl extends ServiceImpl<MonitorMapper, Monitor> implements MonitorService {

    @Autowired
    private MonitorMapper monitorMapper;

    @Autowired
    AlertMapper alertMapper;


    /**
     * @Author luoxin
     * @Description 监测数据接口
     * @Date 3:35 下午 2021/8/20
     * @param carDumperPOJO
     * @return
     */
    @Override
    public ResultInfoV110 insert(CarDumperPOJO carDumperPOJO) {
        log.debug("MonitorServiceImpl类insert方法进入,参数为："+carDumperPOJO);

        String id = carDumperPOJO.getID();
        String dgzt = TypeEnum.getTarget(carDumperPOJO.getDGZT(), "DGZT");
        String yczt = TypeEnum.getTarget(carDumperPOJO.getYCZT(), "YCZT");
        String ggzt = TypeEnum.getTarget(carDumperPOJO.getGGZT(), "GGZT");
        String dgjgxh = TypeEnum.getTarget(carDumperPOJO.getDGJGXH(), "DGJGXH");
        String ycbxh1 = TypeEnum.getTarget(carDumperPOJO.getYCBXH1(), "YCBXH1");
        String ycbxh2 = TypeEnum.getTarget(carDumperPOJO.getYCBXH1(), "YCBXH2");
        String ycbxh3 = TypeEnum.getTarget(carDumperPOJO.getYCBXH1(), "YCBXH3");
        String ycbxh4 = TypeEnum.getTarget(carDumperPOJO.getYCBXH1(), "YCBXH4");
        String aixbxh = TypeEnum.getTarget(carDumperPOJO.getAIXBXH(), "AIXBXH");
        String fzjd = carDumperPOJO.getFZJD();
        String sjsj = carDumperPOJO.getSJSJ();

        if (carDumperPOJO.getDGZT().equals("1")) {
            Alert alert = new Alert();
            alert.setType("DGZT");
            alert.setBjxx(dgzt);
            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime parse = LocalDateTime.parse(sjsj,df);
            alert.setSjsj(parse);
            alert.setId(id);
            int insert = alertMapper.insert(alert);
            //验证是否成功插入数据
            if (insert == 0) {
                throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F001, "添加对轨状态报警数据"));
            }
        }
        if (carDumperPOJO.getYCZT().equals("1")) {
            Alert alert = new Alert();
            alert.setType("YCZT");
            alert.setBjxx(yczt);
            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime parse = LocalDateTime.parse(sjsj,df);
            alert.setSjsj(parse);
            alert.setId(id);
            int insert = alertMapper.insert(alert);
            //验证是否成功插入数据
            if (insert == 0) {
                throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F001, "添加压车状态报警数据"));
            }
        }
        if (carDumperPOJO.getGGZT().equals("1")) {
            Alert alert = new Alert();
            alert.setType("GGZT");
            alert.setBjxx(ggzt);
            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime parse = LocalDateTime.parse(sjsj,df);
            alert.setSjsj(parse);
            alert.setId(id);
            int insert = alertMapper.insert(alert);
            //验证是否成功插入数据
            if (insert == 0) {
                throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F001, "添加挂钩状态报警数据"));
            }
        }

        Monitor monitor = new Monitor();
        monitor.setId(id);
        monitor.setDgzt(dgzt);
        monitor.setYczt(yczt);
        monitor.setGgzt(ggzt);
        monitor.setDgjgxh(dgjgxh);
        monitor.setYcbxh1(ycbxh1);
        monitor.setYcbxh2(ycbxh2);
        monitor.setYcbxh3(ycbxh3);
        monitor.setYcbxh4(ycbxh4);
        monitor.setAisbxh(aixbxh);
        monitor.setFzjd(fzjd);
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime parse = LocalDateTime.parse(sjsj,df);
        monitor.setSjsj(parse);
        int insert = monitorMapper.insert(monitor);
        //验证是否成功插入数据
        if (insert == 0) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F001, "添加监测数据"));
        }

        log.debug("MonitorServiceImpl类insert方法结束");
        return ResultInfoV110.success();
    }

    @Override
    public ResultInfoV110 selectEuipmentHookStatuAllList(String equipmentId, Integer page, Integer size, String StartTime, String endTime) {
        log.info("AlertServiceImpl的selectEquipmentAlertList的方法进入,参数equipmentId为" + equipmentId+",page="+page+",size"+size);
        QueryWrapper<Monitor> monitorQueryWrapper = new QueryWrapper<>();
        monitorQueryWrapper.eq("id", equipmentId)
                .ge(!StrUtil.hasEmpty(StartTime) , "sjsj", StartTime)
                .le(!StrUtil.hasEmpty(endTime), "sjsj", endTime)
                .orderByDesc("sjsj");

        Page<Monitor> monitorPage = new Page<>(page, size);
        Page<Monitor> monitorIPage = monitorMapper.selectPage(monitorPage, monitorQueryWrapper);
        HashMap<Object, Object> resultMap = new HashMap<>();
        List<Monitor> records = monitorIPage.getRecords();

        resultMap.put("list",records);
        resultMap.put("page",page);
        resultMap.put("size",size);
        resultMap.put("total",monitorPage.getTotal());
        resultMap.put("totalPage",monitorPage.getPages());

        return ResultInfoV110.success(resultMap);
    }


    /**
     * @param equipmentId
     * @return List<Monitor>
     * @Decription 根据设备Id查询最新10条设备车钩状态
     */
    @Override
    public ResultInfoV110 selectEuipmentHookStatuList(String equipmentId){
     log.info("MonitorServiceImpl的selectEuipmentHookStatuList方法进入参数equipmentId为"+equipmentId);
     //根据设备Id查询最新30条设备车钩状态
     List<Monitor> monitors = monitorMapper.selectEuipmentHookStatuList(equipmentId);
     return ResultInfoV110.success(monitors);

    }

    /**
     * @param equipmentId 设备Id
     * @Decription 根据设备Id查询最新1条设备压车臂状态
     * @return ResultInfoV110
     */
    @Override
    public ResultInfoV110 selectEuipmentPressArmStatuList(String equipmentId) {
       log.info("MonitorServiceImpl的selectEuipmentPressArmStatuList方法进入,参数equipmentId为"+equipmentId);
       //根据设备Id查询最新30条设备压车臂状态
        List<Monitor> monitors = monitorMapper.selectEuipmentPressArmStatuList(equipmentId);
        return ResultInfoV110.success(monitors);
    }

    /**
     * @param equipmentId
     * @Decription 根据表主键Id查询设备压车臂状态,分页
     * @return List<Monitor>
     */
    @Override
    public ResultInfoV110 selectEuipmentPressArmStatuPageList(MonitorParamPOJO monitorParamPOJO) {
        log.info("MonitorServiceImpl的selectEuipmentPressArmStatuPageList方法进入,参数monitorParamPOJO为"+monitorParamPOJO);

        String equipmentId = monitorParamPOJO.getEquipmentId();
        Integer page = monitorParamPOJO.getPage();
        Integer size = monitorParamPOJO.getSize();
        LocalDateTime searchStartTime = monitorParamPOJO.getSearchStartTime();
        LocalDateTime searchEndTime = monitorParamPOJO.getSearchEndTime();

        //根据表主键Id查询设备压车臂状态,分页
        List<Monitor> monitors = monitorMapper.selectEuipmentPressArmStatuPageList(equipmentId, page, size,searchStartTime,searchEndTime);

        //查询条数
        Integer count = monitorMapper.selectEuipmentPressArmStatuPageListCount(equipmentId,searchStartTime,searchEndTime);

        //封装信息
        MonitorPagePOJO monitorPagePOJO = new MonitorPagePOJO();
        monitorPagePOJO.setPage(page);
        monitorPagePOJO.setSize(size);
        monitorPagePOJO.setTotalCount(count);
        monitorPagePOJO.setTotalPage(PageUtil.getTotalPage(count, size));
        monitorPagePOJO.setMonitorList(monitors);
        return ResultInfoV110.success(monitorPagePOJO);
    }

    /**
     * @param equipmentId 设备Id
     * @Decription 根据设备Id查询最新1条设备轨道状态
     * @return ResultInfoV110
     */
    @Override
    public ResultInfoV110 selectEuipmentTrackStatuList(String equipmentId) {
        log.info("MonitorServiceImpl的selectEuipmentTrackStatuList方法进入,参数equipmentId为"+equipmentId);
        //根据设备Id查询最新30条设备轨道状态
        List<Monitor> monitors = monitorMapper.selectEuipmentTrackStatuList(equipmentId);
        return ResultInfoV110.success(monitors);
    }

    /**
     * @param monitorParamPOJO
     * @Decription 根据表主键Id查询设备轨道状态,分页
     * @return List<Monitor>
     */
    @Override
    public ResultInfoV110 selectEuipmenTrackStatuPageList(MonitorParamPOJO monitorParamPOJO) {
        log.info("MonitorServiceImpl的selectEuipmenTrackStatuPageList方法进入,参数monitorParamPOJO为"+monitorParamPOJO);

        String equipmentId = monitorParamPOJO.getEquipmentId();
        Integer page = monitorParamPOJO.getPage();
        Integer size = monitorParamPOJO.getSize();
        LocalDateTime searchStartTime = monitorParamPOJO.getSearchStartTime();
        LocalDateTime searchEndTime = monitorParamPOJO.getSearchEndTime();

        //根据表主键Id查询设备压车臂状态,分页
        List<Monitor> monitors = monitorMapper.selectEquipmentTrackStatuPageList(equipmentId, page, size,searchStartTime,searchEndTime);

        //查询条数
        Integer count = monitorMapper.selectEuipmentPressArmStatuPageListCount(equipmentId,searchStartTime,searchEndTime);

        //封装信息
        MonitorPagePOJO monitorPagePOJO = new MonitorPagePOJO();
        monitorPagePOJO.setPage(page);
        monitorPagePOJO.setSize(size);
        monitorPagePOJO.setTotalCount(count);
        monitorPagePOJO.setTotalPage(PageUtil.getTotalPage(count, size));
        monitorPagePOJO.setMonitorList(monitors);
        return ResultInfoV110.success(monitorPagePOJO);
    }
}
